package com.net

import androidx.annotation.Keep
import okhttp3.CookieJar
import okhttp3.Interceptor
import java.util.*

/**
 * @describe: 网络客户端配置项，可以扩展
 * @Author: lixiaopeng
 * @Date: 2020/6/24
 */
@Keep
class NetClientConfig private constructor(readTimeOut: Long,
                                          connectTimeout: Long,
                                          writeTimeout: Long,
                                          cookieJar: CookieJar?,
                                          interceptors: MutableList<Interceptor>,
                                          netInterceptors: MutableList<Interceptor>,
                                          debug: Boolean) {
    var readTimeOut: Long = 5000
    var connectTimeout: Long = 5000
    var writeTimeout: Long = 5000
    var cookieJar: CookieJar? = null
    var debug = false
    var interceptors: MutableList<Interceptor>
    var netInterceptors: MutableList<Interceptor>

    class Builder : INetClientConfig {
        private var readTimeOut: Long = 5000
        private var connectTimeout: Long = 5000
        private var writeTimeout: Long = 5000
        private var cookieJar: CookieJar? = null
        private var debug = false
        private val interceptors:MutableList<Interceptor> = mutableListOf()
        private val netInterceptors:MutableList<Interceptor> = mutableListOf()
        fun build(): NetClientConfig {
            return NetClientConfig(readTimeOut, connectTimeout, writeTimeout, cookieJar, interceptors, netInterceptors,debug)
        }

        override fun setReadTimeOut(readTimeOut: Long): Builder {
            this.readTimeOut = readTimeOut
            return this
        }

        override fun setConnectTimeout(connectTimeOut: Long): Builder {
            connectTimeout = connectTimeOut
            return this
        }

        override fun setWriteTimeout(writeTimeOut: Long): Builder {
            writeTimeout = writeTimeOut
            return this
        }

        override fun setCookieJar(cookieJar: CookieJar?): Builder {
            this.cookieJar = cookieJar
            return this
        }

        override fun debug(debug: Boolean): Builder {
            this.debug = debug
            return this
        }

        override fun addInterceptor(interceptor: Interceptor?): Builder {
            interceptor?.let {
                interceptors.add(it)
            }
            return this
        }

        override fun addNetInterceptor(interceptor: Interceptor?): Builder {
            interceptor?.let {
                netInterceptors.add(it)
            }
            return this
        }
    }

    interface INetClientConfig {
        fun setReadTimeOut(readTimeOut: Long): Builder
        fun setConnectTimeout(connectTimeOut: Long): Builder
        fun setWriteTimeout(writeTimeOut: Long): Builder
        fun setCookieJar(cookieJar: CookieJar?): Builder
        fun debug(debug: Boolean): Builder
        fun addInterceptor(interceptor: Interceptor?): Builder
        fun addNetInterceptor(interceptor: Interceptor?): Builder
    }

    init {
        this.readTimeOut = readTimeOut
        this.connectTimeout = connectTimeout
        this.writeTimeout = writeTimeout
        this.cookieJar = cookieJar
        this.interceptors = interceptors
        this.debug = debug
        this.netInterceptors = netInterceptors
    }
}